/*This do file generates a results file containing partysize, number of votes, sumclose, and weighted (by closeness of the vote) and unweighted unity 
and rice scores.  These unity and rice scores are presented both with and without an adjustment for small party bias, and "diff" variables are created
to measure the size of this adjustment.*/


				/* First, we will set up a few parameters, to insure that the stata system is equipped to deal 

				with a large data set.  We will clear all previous data, and set the memory and matrix sizes. */





clear

set memory 110m 

set matsize 800

set maxvar 32766 /*maximum variable size for Stata 7.0 is 2047. if you are running stata 7.0, change "set maxvar" to 2047*/
set more off



				/* Now, we will establish several macros to run the code.  Each macro will be defined at the

				beginning of the code.  When a macro appears throughout the code, it will be surrounded by 

				single quotation marks, such as `macro'.  Once the macros are defined at the beginning of the 

				code, they do not need to be defined again.  There exists only one other place within the code

				that must be adjusted per each data set.  That place is set off with a string of asterisks, 

				and directions follow the asterisks. */







local w "1" 	/* "w" is the number of the first vote in the congress of analysis */

local x "356"		/* "x" is the number of the last vote in that congress */

local y "9"		/* "y" is the highest party code (or other unit) whose unity is being tested */

local z "1"			/* "z" is the lowest party code (or other unit) whose unity is being tested */

local q "356"		/* "q" is the total number of votes in the given data set */

local country "Russia"	/* "country" is the name of the country in the data set */

local congress "19961997"	

				/* "congress" is the number of the congress in question for this country, or any other

				indicator that might designate this data set as different from another regarding this

				country (there might be a "2-1st" designation, for instance, that indicates 2nd congress,

				1st party assignments, as in Peru).  If there is only 

				one congress for this country, the number will always be 1.  If there are more than one

				congress, each time the code is run, this number must be changed.  */

local master "C:\WINDOWS\Desktop\Carey\masterdata/`country'Master`congress'.dta"

				/* "master" is the file name of the master file regarding that country.  It contains a 

				DEPUTY x VOTE matrix, with an observation named THRESH and a variable named PARTY (or whatever 

				unit of analysis there will be) as well.  

				In other words, down the left side of the matrix it should read "THRESH, D001, D002," etc.  

     				Across the top of the matrix should read "party, v1, v2, v3," etc.

				There is NO OTHER information in this master data set.  

				Observations regarding date, issue coding, or any other information must be deleted 

				before this file is named the master file */

local path "c:\temp\"

				/* "path" is the basic path name for all files that this data set will be creating or accessing. 

				It does not name a file, and has no extension.  */

local output "c:\output\" 
			
			/* "output" is the basic path name for the output file that this code generates.  */

local helpfile "C:\WINDOWS\Desktop\Carey\masterdata\helper.dta"

			/* helpfile is the location of a simple dataset called helper.dta, which contains one variable, "help," which is defined as "."  Helpfile is used
				as a starter at one point in the code where it is necessary to create a dataset from scratch.*/

				/* Now that we have defined all relevant macros, we will begin to calculate close scores 

				for each vote.  To do this, we will create a loop that analyzes each vote separately.  */



use "`master'"



forv i=`w'/`x'  {

	keep v`w'-v`x'

	count if v`i'==0

	gen tno`i' = r(N)

	count if v`i'==1 

	gen tyes`i' = r(N)

	count if v`i'==5

	gen tabst`i' = r(N)

	count if v`i'==7

	gen tabsent`i' = r(N)

	count if v`i'==6 

	gen tnoresp`i' = r(N)

	count if v`i'==8

	gen t8`i' = r(N)

	count if v`i'==9

	gen t9`i' = r(N)

	keep in 1

	rename v`i' thresh`i'

	drop v*	

	gen close`i' = 1-(abs((.5)-(tyes`i'/(tyes`i' + tno`i'))))/(.5) if thresh`i'==1

	replace close`i' = 1-(abs((.5)-(tyes`i'/(tyes`i' + tno`i' + tabst`i'))))/(.5) if thresh`i'==2

	replace close`i' = 1-(abs((.67)-(tyes`i'/(tyes`i' + tno`i' + tabst`i'))))/(.67) if thresh`i'==3

	replace close`i' = 1-(abs((.5)-(tyes`i'/(tyes`i' + tno`i' + tabst`i' + tnoresp`i' + tabsent`i' + t8`i' + t9`i'))))/(.5) if thresh`i'==4

	replace close`i' = 1-(abs((.57)-(tyes`i'/(tabsent`i' + tno`i' + tabst`i'+ tnoresp`i' + t8`i' + t9`i'))))/(.57) if thresh`i'==5

	replace close`i' = 1-(abs((.6)-(tyes`i'/(tabsent`i' + tno`i' + tabst`i'+ tnoresp`i' + t8`i' + t9`i'))))/(.6) if thresh`i'==6

	replace close`i' = 1-(abs((.67)-(tyes`i'/(tabsent`i' + tno`i' + tabst`i'+ tnoresp`i' + t8`i' + t9`i'))))/(.67) if thresh`i'==7

	replace close`i' = 1-(abs((.6)-(tyes`i'/(tyes`i' + tno`i' + tabst`i'))))/(.6) if thresh`i'==8

	replace close`i' = 1-(abs((.75)-(tyes`i'/(tyes`i' + tno`i' + tabst`i'))))/(.75) if thresh`i'==9

	replace close`i' = 1-(abs((.71)-(tyes`i'/(tyes`i' + tno`i' + tabst`i'))))/(.71) if thresh`i'==10

	replace close`i' = 1-(abs((.67)-(tyes`i'/(tyes`i' + tno`i' + tabst`i' + tabsent`i'+ tnoresp`i' + t8`i' + t9`i'))))/(.67) if thresh`i'==11

	replace close`i' = 1-(abs((.6)-(tyes`i'/(tyes`i' + tno`i' + tabst`i' + tabsent`i'+ tnoresp`i' + t8`i' + t9`i'))))/(.6) if thresh`i'==12

	drop t*

	save "`path'\`country'`congress'close`i'.dta", replace

	use "`master'", clear

	

}



				/* We have now generated a small close file for each vote. */



clear

use "`path'\`country'`congress'close`w'.dta"

local sclose=close`w'

forv j=`w+1'/`x'   {

	merge using `path'\`country'`congress'close`j'

		if close`j' ~=. {local sclose=`sclose'+close`j'}
	
	drop _merge

}

xpose, clear

gen votenum = _n

rename v1 close

save "`path'\`country'`congress'close.dta", replace



				/* We have now merged all of the small close files into one large close file that contains 

				close scores for every vote under analysis.  We have also created a local variable called sclose which sums the close scores for 
				
				each vote in the dataset.

				Now, we will proceed to calculated weighted and unweighted indices for each cohort, and we 

				will use another loop to do so.		

				*/


 /* D is a measure to adjust rice scores for small party bias, while O is a similar measure for unity scores.  To make this adjustment,
	
	we generate a D and an O for each party on each vote.  We then find the average D and O for each party, counting only votes that the party
	
	"took part in."  For unity scores, this means at least one yes or no vote was cast, and for rice scores, at least two votes.  The average D or O is
	
	then divided by party size. The estimated deviance is then subtracted from the rice or unity score.  This code also generates a mode score "m", which

	is not included in the final output, but could be should it be deemed important.*/



clear

forv j=`z'/`y'   {

	use "`master'", clear

	keep coalition v`w'-v`x'

	egen deptookpart=robs(v`w'-v`x')

	count if coalition==`j' & deptookpart~=0
	
	gen partysize`j'=r(N)

	drop in 1

	gen tookpartr`j'=0

	gen tookpartu`j'=0

	forv i=`w'/`x' {

		count if v`i'==0 & coalition==`j'

		gen n`j'_`i' = r(N)

		count if v`i'==1 & coalition==`j'

		gen y`j'_`i' = r(N)

		count if v`i'==5 & coalition==`j'

		gen ab`j'_`i' = r(N)

		count if (v`i'==7 | v`i'==6 | v`i'==8) & coalition==`j'

		gen nr`j'_`i' = r(N) 

		replace tookpartr`j'= tookpartr`j' +1 if n`j'_`i' + y`j'_`i' >= 2

		replace tookpartu`j'= tookpartu`j' +1 if n`j'_`i' + y`j'_`i' >= 1

		gen D`j'_`i'=(min(y`j'_`i', n`j'_`i'))/(y`j'_`i'+n`j'_`i') if n`j'_`i' + y`j'_`i' > 1

		gen O`j'_`i'=(min(y`j'_`i', n`j'_`i'))/partysize`j'

		gen r`j'_`i'=abs((y`j'_`i'/(y`j'_`i'+n`j'_`i'))-(n`j'_`i'/(y`j'_`i'+n`j'_`i'))) if n`j'_`i' + y`j'_`i' > 1

		gen u`j'_`i'=abs((y`j'_`i'/(y`j'_`i'+n`j'_`i'+ab`j'_`i'+nr`j'_`i'))-(n`j'_`i'/(y`j'_`i'+n`j'_`i'+ab`j'_`i'+nr`j'_`i')))

		gen m`j'_`i'=max((n`j'_`i'/(n`j'_`i'+y`j'_`i'+ab`j'_`i'+nr`j'_`i')), (y`j'_`i'/(n`j'_`i'+y`j'_`i'+ab`j'_`i'+nr`j'_`i')), (( ab`j'_`i'+ nr`j'_`i')/( n`j'_`i'+ y`j'_`i'+ ab`j'_`i'+ nr`j'_`i')))

		drop n`j'_`i'-nr`j'_`i'

	}
keep in 1

keep r* u* m* pa* D* O* took*

save "`path'\`country'`congress'basic`j'.dta", replace

keep pa* D* tookpartr*

egen partyD`j'= rsum(D*)

gen ricedeviance`j'= (partyD`j'/tookpartr`j')/partysize`j'

keep ricedeviance`j'

gen count= _n

save "`path'\`country'`congress'rdev`j'.dta", replace

use "`path'\`country'`congress'basic`j'.dta"

keep pa* O* tookpartu*

egen partyO`j'= rsum(O*)

gen unitydeviance`j'=(partyO`j'/tookpartu`j')/partysize`j'

keep unitydeviance`j'

gen count= _n

save "`path'\`country'`congress'udev`j'.dta", replace

use "`path'\`country'`congress'basic`j'.dta"

drop pa* D* O* took* 


				/* We have now created a basic file for each cohort, containing rice, unity, and mode information

				for each vote.  Now, we will calculate a weighted index for each index that we have.  We will begin

				with rice, then move to unity, then mode.  For rice, all observations are dropped in which fewer than

				two members voted. */


				/*The following code generates a file "r" "u" and "m" for each party that participated in the congress in question.*/	

keep r*

xpose, clear

gen votenum = _n

rename v1 r`j'

merge using "`path'\`country'`congress'close"

drop _merge

keep in 1/`q' if r`j'~=. 

count

	if `r(N)'~=0 {

gen cr`j' = close * r`j'

keep c*

xpose, clear varname

egen sum = rsum(v*)

keep sum _varname

xpose, clear varname

gen wr`j' = cr`j' / close

keep w* 

xpose, clear

rename v1 wtdr`j'

gen count = _n

save "`path'\`country'`congress'r`j'.dta", replace

}


use "`path'\`country'`congress'basic`j'.dta", replace 

keep u*

xpose, clear

gen votenum = _n

rename v1 u`j'

merge using "`path'\`country'`congress'close"

drop _merge

keep in 1/`q' if u`j'~=.

count

	if `r(N)'~=0 {

gen cu`j' = close * u`j'

keep c*

xpose, clear varname

egen sum = rsum(v*)

keep sum _varname

xpose, clear varname

gen wu`j' = cu`j' / close

keep w*

xpose, clear

rename v1 wtdu`j'

gen count = _n

save "`path'\`country'`congress'u`j'.dta", replace
}

use "`path'\`country'`congress'basic`j'.dta", replace

keep m*

xpose, clear

gen votenum = _n

rename v1 m`j'

merge using "`path'\`country'`congress'close"

drop _merge

keep in 1/`q' if m`j'~=.

count

	if `r(N)'~=0 {

gen cm`j' = close * m`j'

keep c*

xpose, clear varname

egen sum = rsum(v*)

keep sum _varname

xpose, clear

gen wm`j' = cm`j' / close

keep w* 

xpose, clear

rename v1 wtdm`j'

gen count = _n

save "`path'\`country'`congress'm`j'.dta", replace

}

/*The following code creates adjusted weighted rice and unity by merging the rdev and udev files, which exist for each party, with the r and u files,
	which exist only for parties that participated.  If the merge is successful, the adjusted index is calculated and saved as an adjwtdr or adjwtdu file.*/
clear

use `path'\`country'`congress'rdev`j'.dta

capture merge using `path'\`country'`congress'r`j'.dta

if _rc==0 {

drop  _merge

gen adjustedwtdr`j'=wtdr`j'-ricedeviance`j'

keep adjust* count

save "`path'\`country'`congress'adjwtdr`j'.dta", replace

}

use `path'\`country'`congress'udev`j'.dta

capture merge using `path'\`country'`congress'u`j'.dta

if _rc==0 {

drop  _merge

gen adjustedwtdu`j'=wtdu`j'-unitydeviance`j'

keep adjust* count

save "`path'\`country'`congress'adjwtdu`j'.dta", replace

}

/*The following code attempts to open the file "r" for party j.  If this file does not exist, helpfile is used to open a blank dataset, and weightedr for party j
is set to "." */

capture use `path'\`country'`congress'r`j'.dta

if _rc~=0 {

clear

use `helpfile'

gen wtdr`j'=.

drop help
}

/*The following code attempts to merge the info for weightedr into  the file "u" for party j.  If this file does not exist, weightedu for party j
is set to "." */

capture merge using `path'\`country'`congress'u`j'.dta

if _rc==0 {drop  _merge}

else {

	gen wtdu`j'=.
}

/*The following code attempts to merge the info for weightedr and weightedu into  the file "m" for party j.  If this file does not exist, weightedm for party j
is set to "." */

capture merge using `path'\`country'`congress'm`j'.dta

if _rc==0 {drop  _merge}

else {

	gen wtdm`j'=.
}

/*The following code attempts to merge the previous info into  the file "adjwtdr" for party j.  If this file does not exist, adjweightedr for party j
is set to "." */

capture merge using `path'\`country'`congress'adjwtdr`j'.dta

if _rc==0 {drop  _merge}

else {

	gen adjwtdr`j'=.
}

/*The following code attempts to merge the previous info into  the file "adjwtdu" for party j.  If this file does not exist, adjweightedu for party j
is set to "."  Finally, all of this gathered info is saved as the wtd file */

capture merge using `path'\`country'`congress'adjwtdu`j'.dta

if _rc==0 {drop  _merge}

else {

	gen adjwtdu`j'=.
}

keep w* adj*

xpose, clear

rename v1 wtd`j'

gen count=_n

save "`path'\`country'`congress'wtd`j'.dta", replace

continue
}

				

				/* We have now generated a file of weighted indices for each cohort.  We will move on to generate

				unweighted indices. */


/*The following code creates variables r u and m for each party on each vote*/


use "`master'", clear

forv j=`z'/`y'   {

	keep coalition v`w'-v`x'

	drop in 1

	forv i=`w'/`x' {

		count if v`i'==0 & coalition==`j'

		gen n`j'_`i' = r(N)

		count if v`i'==1 & coalition==`j'

		gen y`j'_`i' = r(N)

		count if v`i'==5 & coalition==`j'

		gen ab`j'_`i' = r(N)

		count if (v`i'==7 | v`i'==6 | v`i'==8) & coalition==`j'

		gen nr`j'_`i' = r(N) 

		gen r`j'_`i'=abs((y`j'_`i'/(y`j'_`i'+n`j'_`i'))-(n`j'_`i'/(y`j'_`i'+n`j'_`i'))) if n`j'_`i' + y`j'_`i' > 1

		gen u`j'_`i'=abs((y`j'_`i'/(y`j'_`i'+n`j'_`i'+ab`j'_`i'+nr`j'_`i'))-(n`j'_`i'/(y`j'_`i'+n`j'_`i'+ab`j'_`i'+nr`j'_`i')))

		gen m`j'_`i'=max((n`j'_`i'/(n`j'_`i'+y`j'_`i'+ab`j'_`i'+nr`j'_`i')), (y`j'_`i'/(n`j'_`i'+y`j'_`i'+ab`j'_`i'+nr`j'_`i')), (( ab`j'_`i'+ nr`j'_`i')/( n`j'_`i'+ y`j'_`i'+ ab`j'_`i'+ nr`j'_`i')))

		drop n`j'_`i'-nr`j'_`i'

	}

keep in 1

keep r* u* m*

/*The following code averages r u and m for each party, and saves the unweighted averages as the unwtd file*/

foreach v in r u m { egen avg`v' = rmean(`v'`j'*) }

drop r* m* u*

xpose, clear varname

rename v1 unwtd`j'

rename _varname indicator

save "`path'\`country'`congress'unwtd`j'.dta", replace

use "`master'", clear

}

/*The following code uses the unwtd file for each party, keeps only data on rice scores, and merges this with the rdev file for each party.  It then
subtracts ricedeviance from r to determine adjusted unweighted rice, and saves this data in the adjr file.*/

forv j=`z'/`y'   {

use "`path'\`country'`congress'unwtd`j'.dta"

xpose, clear

keep in 1

drop v2 v3

rename v1 r`j'

gen str9 indicator= "adjustedr"

merge using `path'\`country'`congress'rdev`j'

gen unwtd`j' = r`j' - ricedeviance`j'

keep unwtd* indicator

save "`path'\`country'`congress'adjr`j'.dta", replace

/*The following code uses the unwtd file for each party, keeps only data on unity scores, and merges this with the udev file for each party.  It then
subtracts unitydeviance from u to determine adjusted unweighted unity, and saves this data in the adju file.*/

clear

use "`path'\`country'`congress'unwtd`j'.dta"

xpose, clear

keep in 1

drop v1 v3

rename v2 u`j'

gen str9 indicator= "adjustedu"

merge using `path'\`country'`congress'udev`j'

gen unwtd`j' = u`j' - unitydeviance`j'

keep unwtd* indicator

save "`path'\`country'`congress'adju`j'.dta", replace

clear

}

/*The following code creates a file called partysize which, surprisingly, contains the number of each party and its size*/

forv j=`z'/`y'   {

	use "`path'\`country'`congress'basic`j'.dta", replace

	keep partysize*

	rename partysize`j' partysize

	gen coalition=`j'

	save "`path'\`country'`congress'partysize`j'.dta", replace 

	clear

}

/*The following code combines all of the partysize data in one file*/

use "`path'\`country'`congress'partysize`z'.dta"

forv j=`z+1'/`y'   {

	append using "`path'\`country'`congress'partysize`j'.dta"
}

drop in 1

save "`path'\`country'`congress'partysize.dta", replace

clear

/*The following code combines the unweighted indices with the adjusted unweighted indices, and then saves this additional data into the unwtd file*/

forv j=`z'/`y'   {

	use "`path'\`country'`congress'unwtd`j'.dta"

	append using `path'\`country'`congress'adjr`j'

	append using `path'\`country'`congress'adju`j'

	save "`path'\`country'`congress'unwtd`j'.dta", replace

}

/*The following code merges the unwtd file for each party into one big file called totalunwtd */

clear

use "`path'\`country'`congress'unwtd`z'.dta"

forv j=`z+1'/`y'   {

	merge using `path'\`country'`congress'unwtd`j'

	drop _merge

}

gen count = _n

save "`path'\`country'`congress'totalunwtd.dta", replace



				/* We have now generated a file of unweighted indices for all cohorts in the data set.

				Next, we will merge all weighted indices into the same set, to create one total set of 

				weighted and unweighted indices.  */






forv j=`z'/`y'   {

	merge using `path'\`country'`congress'wtd`j'
	drop _merge
}

keep un* w* count

save "`path'\`country'`congress'totalwtd.dta", replace 


/*The following code merges partysize data into totalwtd, keeps only the unweighted data, transposes the format, renames each variable, 
and saves the results in the indices file*/

keep u*

xpose, clear

merge using "`path'\`country'`congress'partysize.dta"

drop _merge

drop v3

rename v1 unwtdrice

rename v2 unwtdunity

rename v4 adjunwtdrice

rename v5 adjunwtdunity

rename coalition partynum

gen coalition = _n

save "`path'\`country'`congress'indices.dta", replace

/*The following code keeps only the weighted indices, transposes the format, renames the variables, and merges this data into the indices file*/

use "`path'\`country'`congress'totalwtd.dta", replace 

keep w*

xpose, clear

drop v3

rename v1 wtdrice

rename v2 wtdunity

rename v4 adjwtdrice

rename v5 adjwtdunity

gen coalition = _n

merge using "`path'\`country'`congress'indices.dta"

drop _merge

drop coalition

/*The following code gets rid of any party for which no indices have been created, and generates "diff" variables which measure the effects of
the adjustment to correct for small party bias*/

drop if wtdrice==. && wtdunity==. && adjwtdrice==. && adjwtdunity==. && unwtdrice==. && unwtdunity==. && adjunwtdrice==. && adjunwtdunity==.

rename partynum coalition

gen diffurice=unwtdrice-adjunwtdrice

gen diffuunity=unwtdunity-adjunwtdunity

gen diffwrice=wtdrice-adjwtdrice

gen diffwunity=wtdunity-adjwtdunity

gen str20 country="`country'"

gen str20 years="`congress'"

gen numvotes=`q'

gen sumClose=`sclose'

order  country years  coalition partysize unwtdrice adjunwtdrice diffurice wtdrice adjwtdrice diffwrice   unwtdunity adjunwtdunity diffuunity wtdunity adjwtdunity diffwunity

save "`output'\`country'`congress'final.dta", replace




